package ecnu.liwaTest.dd.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import static ecnu.liwaTest.dd.DriverSingleton.*;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class HomePage extends BasePage{

    WebDriver driver = getChromeDriver();
    JavascriptExecutor javascriptExecutor = getJs();

    public HomePage goHomePage() {
        driver.findElement(By.cssSelector(".el-menu-item:nth-child(1) > span")).click();
        waitForWindow(1000);
        assertThat(driver.getTitle(), is("首页"));
        return this;
    }

    public HomePage changePassword(String original, String newPass, String againPass) {
        driver.findElement(By.xpath("/html/body/div[1]/div/div[1]/span/span/div")).click();
        driver.findElement(By.xpath("//div/div[@class = 'menuActionItem']/span[1]")).click();
        driver.findElement(By.cssSelector(".el-row:nth-child(1) .el-input__inner")).click();
        driver.findElement(By.cssSelector(".el-row:nth-child(1) .el-input__inner")).sendKeys(original);
        driver.findElement(By.cssSelector(".el-row:nth-child(2) .el-input__inner")).sendKeys(newPass);
        driver.findElement(By.cssSelector(".el-row:nth-child(3) .el-input__inner")).sendKeys(againPass);
        driver.findElement(By.cssSelector(".confirm > span")).click();

        {
            WebElement element = driver.findElement(By.cssSelector(".confirm > span"));
            Actions builder = new Actions(driver);
            builder.moveToElement(element).perform();
        }
        {
            WebElement element = driver.findElement(By.tagName("body"));
            Actions builder = new Actions(driver);
            builder.moveToElement(element, 0, 0).perform();
        }
        return this;
    }

    public HomePage logOut() {
        driver.findElement(By.xpath("/html/body/div[1]/div/div[1]/span/span/div")).click();
        waitForWindow(1000);
        driver.findElement(By.xpath("//div/div[@class = 'menuActionItem']/span[2]")).click();

        waitForWindow(1000);
        assertThat(driver.getTitle(), is("登录"));
        return this;
    }

    public HomePage homeError(String mes) {
        waitForWindow(1000);
        String message = driver.findElement(By.xpath("/html/body/div[1]/div/div[3]/div/div[2]/form/div[3]/div/div/div/div[2]")).getText();
        assertThat(message, is(mes));
        return this;
    }

    public HomePage checkTodayConversation() {
        driver.findElement(By.cssSelector(".el-menu-item:nth-child(4) > span")).click();
        driver.findElement(By.cssSelector(".el-range-input:nth-child(2)")).click();
        clickToday();
//        waitForWindow(1000);
//        driver.findElement(By.cssSelector(".is-left .el-date-table__row:nth-child(4) > .available:nth-child(4) span")).click();
//        waitForWindow(1000);
//        driver.findElement(By.cssSelector(".is-left .el-date-table__row:nth-child(4) > .available:nth-child(5) span")).click();
        waitForWindow(1000);
        driver.findElement(By.cssSelector(".is-plain > span")).click();
        waitForWindow(1000);

        List<WebElement> webElementList = driver.findElements(By.cssSelector(".el-table__row"));
        goHomePage();
        int num = Integer.parseInt(driver.findElement(By.cssSelector(".el-col:nth-child(1) > span:nth-child(2)")).getText());
        assertThat(num, is(webElementList.size()));
        return this;
    }

    public HomePage checkTodayCounselingTime() throws ParseException {
        goHomePage();
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        Date date = sdf.parse(driver.findElement(By.cssSelector(".el-col:nth-child(2) > span:nth-child(2)")).getText());
        Calendar todayCounselingTime = Calendar.getInstance();
        todayCounselingTime.setTime(date);

        //gohelpPage
        driver.findElement(By.cssSelector(".el-menu-item:nth-child(4) > span")).click();
        //search today
        driver.findElement(By.cssSelector(".el-range-input:nth-child(2)")).click();
        clickToday();
//        waitForWindow(1000);
//        driver.findElement(By.cssSelector(".is-left .el-date-table__row:nth-child(4) > .available:nth-child(3) span")).click();
//        waitForWindow(1000);
//        driver.findElement(By.cssSelector(".is-left .el-date-table__row:nth-child(4) > .available:nth-child(4) span")).click();
        waitForWindow(1000);
        driver.findElement(By.cssSelector(".is-plain > span")).click();

        //collect time
        Calendar sum = new GregorianCalendar();
        sum.setTime(sdf.parse("00:00:00"));
        waitForWindow(1000);

        WebElement table = driver.findElement(By.cssSelector("tbody"));
        List<WebElement> elementList = table.findElements(By.className("el-table__row"));

        for(WebElement element:elementList) {
            waitForWindow(1000);
            List<WebElement> times = element.findElements(By.tagName("td"));
            Calendar calendar = new GregorianCalendar();
            waitForWindow(1000);
            calendar.setTime(sdf.parse(times.get(3).getText()));
            sum.add(GregorianCalendar.HOUR, calendar.get(GregorianCalendar.HOUR));
            sum.add(GregorianCalendar.MINUTE, calendar.get(GregorianCalendar.MINUTE));
            sum.add(GregorianCalendar.SECOND, calendar.get(GregorianCalendar.SECOND));
            //System.out.println(calendar);
        }
        assertThat(todayCounselingTime, is(sum));
        return this;
    }

    public void checkAllCounseling(){
        goHomePage();
        driver.findElement(By.cssSelector(".el-col > .el-button:nth-child(2) > span")).click();
        waitForWindow(1000);
        int getSum = Integer.parseInt(driver.findElement(By.cssSelector(".rightCount > span:nth-child(2)")).getText());

        driver.findElement(By.cssSelector(".el-menu-item:nth-child(4) > span")).click();

        waitForWindow(1000);
        WebElement table = driver.findElement(By.cssSelector("tbody"));
        List<WebElement> elementList = table.findElements(By.className("el-table__row"));

        int sum = elementList.size();

        WebElement arrow = driver.findElement(By.className("btn-next"));
        while(arrow.isEnabled()) {
            arrow.click();
            waitForWindow(2000);
            table = driver.findElement(By.cssSelector("tbody"));
            elementList = table.findElements(By.className("el-table__row"));
            sum+=elementList.size();
        }

        assertThat(sum, is(getSum));

    }

    public HomePage checkConversation(){
        goHomePage();
        waitForWindow(1000);
        List<WebElement> elements = driver.findElements(By.className("conversation-item-container"));
        int cons = elements.size();
        waitForWindow(1000);
        driver.findElement(By.cssSelector(".el-col > .el-button:nth-child(2) > span")).click();
        waitForWindow(1000);
        int num = Integer.parseInt(driver.findElement(By.cssSelector(".el-col:nth-child(3) > span:nth-child(2)")).getText());

        assertThat(cons, is(num));
        return this;
    }


}
